<template>
  <el-dialog
    :close-on-click-modal="false"
    title="质量奖惩"
    :visible.sync="show"
    width="1000px"
    center
  >
    <el-form ref="form" :rules="rules" style="padding-top: 10px;" :model="form" label-width="120px">
      <div class="com-form-title">基础信息</div>
      <el-row>
        <el-col :span="8">
          <el-form-item label="项目名称" prop="projectName">
            <el-input v-model="form.projectName" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="奖惩时间" prop="rpTime">
            <el-date-picker v-model="form.rpTime" style="width:100%;" type="date" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="奖惩类型" prop="rpType">
            <el-select v-model="form.rpType" :disabled="disableForm" style="width: 100%;" placeholder="请选择奖惩类型">
              <el-option v-for="item in qualityRewardsPunishmentRpType" :key="item.name" :label="item.name" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="奖惩单位/个人" prop="rpUnit">
            <el-input v-model="form.rpUnit" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="奖惩依据" prop="rpCond">
            <el-input v-model="form.rpCond" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="奖惩金额" prop="rpAmount">
            <el-input v-model="form.rpAmount" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="奖惩审核" prop="rpCheck">
            <el-input v-model="form.rpCheck" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <!-- <el-col :span="8">
          <el-form-item label="经手人" prop="dealer">
            <el-input v-model="form.dealer" :disabled="disableForm" />
          </el-form-item>
        </el-col> -->
        <el-col :span="8">
          <el-form-item label="审核" prop="check">
            <el-input v-model="form.basCheck" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="签发" prop="sign">
            <el-input v-model="form.sign" :disabled="disableForm" />
          </el-form-item>
        </el-col>

      </el-row>
      <el-form-item label="奖惩原因" prop="rpReason">
        <el-input v-model="form.rpReason" :rows="4" :disabled="disableForm" type="textarea" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark" :rows="4" :disabled="disableForm" type="textarea" />
      </el-form-item>

      <!-- <div class="com-form-title">附件</div>
      <el-form-item label="文件上传">
        <uploader v-model="fileList" :disabled="disableForm" :can-download="optType==='detail'" />
      </el-form-item> -->
      <div class="com-form-title">附件</div>
      <el-button style="margin-top:10px;" type="primary" @click="showFileModal = true">附件资料({{ fileList.length }})</el-button>
      <file-uploader v-model="fileList" :show-modal.sync="showFileModal" :disabled="disableForm" :can-download="optType==='detail'" />
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="show = false">取 消</el-button>
      <el-button type="primary" @click="saveDoc">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addQualityRewardsPunishment, updateQualityRewardsPunishment, getQualityRewardsPunishmentDetail } from '@/api/qualityManage'
import { getAreaList } from '@/api/common'
import { qualityRewardsPunishmentRpType } from '@/utils/dicData'
import fileUploader from '@/components/FileUploader'
export default {
  name: 'AddQualityRewardsPunishment',
  components: {
    fileUploader
  },
  props: {
    showModal: {
      type: Boolean,
      default: false
    },
    optType: {
      type: String,
      default: 'add'
    },
    dir: {
      type: Object,
      default() {
        return {}
      }
    },
    data: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      qualityRewardsPunishmentRpType,
      showFileModal: false,
      addType: '', // same   under
      visible: false,
      showAdd: false,
      selectedNode: null,
      form: {
        projectName: '',
        rpTime: '',
        rpCond: '',
        rpAmount: '',
        rpType: '',
        rpUnit: '',
        rpReason: '',
        dealer: '',
        rpCheck: '',
        sign: '',
        remark: '',
        createTime: '',
        basCheck: ''
      },
      fileList: [
      ],
      rules: {
        projectName: [
          { required: true, message: '项目名称', trigger: 'change' }
        ],
        rpTime: [
          { required: true, message: '奖惩时间', trigger: 'change' }
        ],
        rpType: [
          { required: true, message: '奖惩类型', trigger: 'change' }
        ],
        rpUnit: [
          { required: true, message: '奖惩单位/个人', trigger: 'change' }
        ],
        // rpCond: [
        //   { required: true, message: '奖惩依据', trigger: 'change' }
        // ],
        rpAmount: [
          { required: true, message: '奖惩金额', trigger: 'change' }
        ],
        // rpCheck: [
        //   { required: true, message: '奖惩审核', trigger: 'change' }
        // ],
        dealer: [
          { required: true, message: '经手人', trigger: 'change' }
        ],
        basCheck: [
          { required: true, message: '审核', trigger: 'change' }
        ]
        // sign: [
        //   { required: true, message: '签发', trigger: 'change' }
        // ],
        // rpReason: [
        //   { required: true, message: '奖惩原因', trigger: 'change' }
        // ]
      },
      provinceData: [],
      cityData: []
    }
  },
  computed: {
    disableForm() {
      return this.optType === 'detail'
    },
    show: {
      get() {
        return this.showModal
      },
      set(val) {
        this.$emit('update:showModal', val)
      }
    }
  },
  watch: {
    showModal(val) {
      if (val && this.optType !== 'add') {
        this.getDocDetail(this.data.id)
        this.$nextTick(() => {
          Object.keys(this.form).forEach(key => {
            const source = this.data[key]
            if (typeof source === 'boolean') {
              this.form[key] = source ? 1 : 0
            } else {
              this.form[key] = source
            }
          })
        })
      } else if (!val) {
        this.fileList = []
        this.$refs.form.resetFields()
      }
    }
  },
  mounted() {
    this.getRegionData()
  },
  methods: {
    provinceChange(e) {
      const selectedData = this.provinceData.filter(val => val.name === e)[0]
      if (selectedData) {
        this.form.city = ''
        const params = {
          parentCode: selectedData.code,
          areaType: 2
        }
        getAreaList(params).then(res => {
          this.cityData = res.data || []
        })
      }
    },
    getDocDetail(id) {
      getQualityRewardsPunishmentDetail({ qualityRewardsPunishmentId: id }).then(res => {
        const { lstAttachment } = res.data || {}
        if (lstAttachment && lstAttachment.length > 0) {
          this.fileList = lstAttachment
        }
      })
    },
    getRegionData() {
      getAreaList({ areaType: '1' }).then(res => {
        this.provinceData = res.data || []
      })
    },
    saveDoc() {
      if (this.optType === 'detai') {
        this.show = false
        return
      }
      this.$refs.form.validate((valid) => {
        if (valid) {
          const params = {
            qualityRewardsPunishment: {
              ...this.form,
              ...this.optType === 'edit' ? {
                id: this.data.id
              } : {}
            },
            lstAttachment: this.fileList
          }
          const fn = this.optType === 'add' ? addQualityRewardsPunishment : updateQualityRewardsPunishment
          fn(params).then(res => {
            if (res.code === '0001') {
              this.$message({
                type: 'success',
                message: '保存成功!'
              })
              this.show = false
              this.$emit('freshList')
              this.$refs.form.resetFields()
            } else {
              this.$message({
                type: 'error',
                message: res.msg
              })
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.com-form-title{
  font-size: 14px;
  font-weight: bold;
  line-height: 35px;
  color: #409EFF;
  border-bottom: 1px solid #dedede;
  margin-bottom: 6px;
}
</style>
